<template>
  <div id="cart">
    <nav-bar class="header">
      <div slot="left" @click="goBack()" class="back-icon">
        <img src="~assets/img/common/back.svg" alt="">
      </div>
      <div class="center" slot="center">
        购物车
      </div>
    </nav-bar>
    <!-- <cart-list class="cart_list"/>
    <cart-tool></cart-tool> -->
    <div class="cart-body">
      <van-checkbox-group @change="groupChange" v-model="result" ref="checkboxGroup">
        <van-swipe-cell :right-width="50" v-for="(item,index) in list" :key="index">
          <div class="good-item">
            <van-checkbox :name="item.cartItemId" checked-color="#ff8198"/>
            <div class="good-img">
              <img :src="prefix(item.goodsCoverImg)" alt="">
            </div>
            <div class="good-desc">
              <div class="good-title">
                <span>{{ item.goodsName }}</span>
                <span>x{{ item.goodsCount }}</span>
              </div>
              <div class="good-btn">
                <div class="price">¥{{ item.sellingPrice }}</div>
                <van-stepper
                  integer
                  :min="1"
                  :value="item.goodsCount"
                  :name="item.cartItemId"
                  async-change
                  @change="onChange"
                />
              </div>
            </div>
          </div>
          <van-button
            slot="right"
            square
            icon="delete"
            type="danger"
            class="delete-button"
            @click="deleteGood(item.cartItemId)"
          />
        </van-swipe-cell>
      </van-checkbox-group>
    </div>
    <van-submit-bar 
      v-if="list.length > 0"
      class="submit-all"
      :price="total * 100"
      button-text="结算"
      @submit="onSubmit">
      <van-checkbox @click="allCheck" v-model="checkAll" checked-color="#ff8198">全选</van-checkbox>
    </van-submit-bar>  
     <div class="empty" v-if="!list.length">
      <van-icon name="smile-o" />
      <div class="title">购物车 空空如也</div>
      <van-button color="#ff8198" type="primary" @click="goTo" block>前往首页</van-button>
    </div>
  </div>
</template>

<script>
import NavBar from 'components/common/navbar/NavBar'
import { getCart, deleteCartItem, modifyCart } from 'network/cart'

// import CartList from './childcoms/CartList'
// import CartTool from './childcoms/CartTool'
// import {mapGetters} from 'vuex'

import Vue from 'vue'
import {  Checkbox,CheckboxGroup,SwipeCell,Stepper,Toast,SubmitBar,Icon,Button } from 'vant'
Vue.use(Checkbox).use(CheckboxGroup).use(SwipeCell).use(Stepper).use(SubmitBar).use(Icon).use(Button)
export default {
  name:'cart',
  components:{
    NavBar,
    // CartList,
    // CartTool
  },
  data() {
    return {
      list:[],
      result: [],
      
      checkAll: true
    }
  },
  mounted() {
    this.init()
  },
  computed:{
    // ...mapGetters(['cartLength'])
    total: function() {
      let sum = 0
      let _list = this.list.filter(item => this.result.includes(item.cartItemId))
      _list.forEach(item => sum += item.goodsCount * item.sellingPrice)
      return sum
    }
  },
  methods: {
    goBack() {
       this.$router.go(-1)
    },
    goTo() {
      this.$router.push({ path: 'home' })
    },
    async init() {
      Toast.loading({ message: '加载中....', forbidClick: true});
      const { data } = await getCart({ pageNUmber: 1 })
      this.list = data;
      this.result = data.map(item => item.cartItemId)
      Toast.clear()
      console.log(data);
    },
    async onChange(value, detail) {
      if (this.list.filter(item => item.cartItemId == detail.name)[0].goodsCount == value) return
      Toast.loading({ message: '修改中...', forbidClick: true });
      const params = {
        cartItemId: detail.name,
        goodsCount: value
      }
      const { data } = await modifyCart(params)
      this.list.forEach(item => {
        if (item.cartItemId == detail.name) {
          item.goodsCount = value
        }
      })
      Toast.clear();
    },
    async onSubmit() {
      if (this.result.length == 0) {
        Toast.fail('请选择商品进行结算')
        return
      }
      const params = JSON.stringify(this.result)
      // for(let i = 0; i < this.result.length; i++) {
      //   await deleteCartItem(this.result[i])
      // }
      this.$router.push({ path: `create-order?cartItemIds=${params}` })
    },
    async deleteGood(id) {
      const { data } = await deleteCartItem(id)
      this.$store.dispatch('updateCart')
      this.init()
    },
    groupChange(result) {
      if (result.length == this.list.length) {
        this.checkAll = true
      } else {
        this.checkAll = false
      }
      this.result = result
    },
    allCheck(value) {
      if(!this.checkAll){
        this.result = this.list.map(item => item.cartItemId)
      }else{
        this.result = []
      }
    }
  }
}
</script>

<style scoped lang="less">
.cart-body {
  margin: 64px 0 100px;
  padding-left: 10px;
  .good-item {
    display: flex;
    .good-img {
      img {
        width: auto;
        height: 80px;
        object-fit: cover;
      }
    }
    .good-desc {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      flex: 1;
      padding: 20px;
      .good-title {
        display: flex;
        justify-content: space-between;
      }
      .good-btn {
        display: flex;
        justify-content: space-between;
        .price {
          font-size: 16px;
          color: red;
          line-height: 28px;
        }
        .van-icon-delete {
          font-size: 20px;
          margin-top: 4px;
        }
      }
    }
  }
  .delete-button {
    width: 50px;
    height: 100%;
  }
}
.empty {
  width: 50%;
  margin: 0 auto;
  text-align: center;
  margin-top: 200px;
  .van-icon-smile-o {
    font-size: 50px;
  }
  .title {
    font-size: 16px;
    margin-bottom: 20px;
  }
}
.submit-all {
  margin-bottom: 50px;
  .van-checkbox {
    margin-left: 10px
  }
  .van-submit-bar__text {
    margin-right: 10px
  }
  .van-submit-bar__button {
    background: #ff8198;
  }
}
.van-checkbox__icon--checked .van-icon {
  background-color: #ff8198;
  border-color: #ff8198;
}
  

</style>